home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_12 / guthrie2 / speed_up.c < prev    next >
C/C++ Source or Header  |  1994-01-24  |  6KB  |  162 lines

  1. /**************************************************************************
  2. *  File Name     : SPEED_UP.C
  3. *  Description   : Preprocessor utility for the Xlate System.
  4. *                  Creates Binary versions of the Translate File.
  5. *  Author        : R. Scott Guthrie  /  All Rights Reserved
  6. *  Compile\Link  : Link this source code with XLATE.OBJ
  7. **************************************************************************/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <io.h>
  12. #include <fcntl.h>
  13. #include <alloc.h>
  14. #include <assert.h>
  15. #include "xlate.h"
  16.  
  17. /* Local Prototypes */
  18. void instructions(void);
  19. int open_files(const char *filename, FILE **tf, FILE **bf);
  20.  
  21. /* Global Variables */
  22. extern XLATE *XlateBase;
  23. extern int XlateLines;
  24. extern char *XlateFile;
  25.  
  26. /*=======================*/
  27.   void instructions(void)
  28. /*=======================*/
  29. /* 'instructions()' is called if the user does  */
  30. /* not specify filename to be converted.        */
  31. {
  32.   /* Print instructions for the user. */
  33.   printf(" ##############\n #  SPEED_UP  #\n ##############\n");
  34.   printf("The 'SPEED_UP' program builds a version of XLATE's translate\n");
  35.   printf("file that loads faster then the text version.\n\n");
  36.   printf("    Syntax:   SPEED_UP filename\n\n");
  37.   printf("The parameter 'filename' is the name of the Text Based\n");
  38.   printf("('.TRN') Translate File.  (The filename extension of '.TRN'\n");
  39.   printf("MUST NOT be entered as part of the filename.)\n\n");
  40.   printf("    Example:  SPEED_UP AMERICAN\n\n");
  41.   printf("The example will use the file 'AMERICAN.TRN' to build a faster\n");
  42.   printf("loading version that will have an extension of '.TRB'.\n\n");
  43.   printf("If both the '.TRN' and '.TRB' (speed-up) versions of the\n");
  44.   printf("translate file exist, XLATE will use the speed-up version\n");
  45.   printf("at run time.\n");
  46.   return;
  47. }
  48.  
  49. /*==========================================================*/
  50.   int open_files(const char *filename, FILE **tf, FILE **bf)
  51. /*==========================================================*/
  52. /* Open the Text File and the new Speed-up file */
  53. {
  54.   char tfname[128];     /* Name of the Source File */
  55.   char bfname[128];   /* Name of the Destination File */
  56.  
  57.   /* Open the Text (Source) File for Reading. */
  58.   sprintf(tfname, "%s.TRN", filename);
  59.   if((*tf = fopen(tfname, "r")) == NULL)
  60.   {
  61.     printf("Speed_up: Error opening file %s for reading.\n", tfname);
  62.     return 0;  /* fail */
  63.   }
  64.  
  65.   /* Open the Binary File for Writing. (Overwrite if it exists) */
  66.   sprintf(bfname, "%s.TRB", filename);
  67.   if((*bf = fopen(bfname, "wb")) == NULL)
  68.   {
  69.     printf("Speed_up: Error creating or opening file %s for writing.\n",
  70.         bfname);
  71.     return 0;  /* fail */
  72.   }
  73.   return 1;    /* success */
  74. }
  75.  
  76. /*================================*/
  77.   void main(int argc, char **argv)
  78. /*================================*/
  79. {
  80.   FILE  *tf;                    /* Text File handle              */
  81.   FILE  *bf;                    /* Binary File handle            */
  82.   int   i;                      /* Local Index Variable          */
  83.   XLATE *node;                  /* Translate Node                */
  84.   int   length;                 /* strlen variable.              */
  85.   int   retval;                 /* Return value variable.        */
  86.  
  87.   /* Check the parameters.  There should be 2. The Program name  */
  88.   /* and the File Name (without the extension).                  */
  89.   if(argc != 2)
  90.   {
  91.     instructions();
  92.     exit(0);
  93.   }
  94.  
  95.   /* Open (or Create) the Binary File for Writing. */
  96.   if(open_files(argv[1], &tf, &bf))
  97.   {
  98.     printf("\nSpeed_up: Begin Scan...\n");
  99.  
  100.     /* Build Translation table */
  101.     retval = XlateLoadText(tf);
  102.     if(retval == -1)
  103.     {
  104.       /* memory allocation error */
  105.       fclose(tf);     /* Close Text Translate File (input)    */
  106.       fclose(bf);     /* Close Binary Translate File (output) */
  107.       printf("Speed_up: Memory Allocation Error.\n");
  108.       exit(0);  /* stop processing */
  109.     }
  110.     fclose(tf);       /* Finished with Text File */
  111.  
  112.     /* Report progress... */
  113.     printf("Speed_up: %d Translate lines found.\n", retval);
  114.     printf("Speed_up: Generating Binary File...\n");
  115.  
  116.     /* In-Memory table generated.  Read data from it into Binary File */
  117.  
  118.     /* Format of the Binary Version of the Translate File:               */
  119.     /*  File signiture  (char[4] = ".TRB")                               */
  120.     /*  Number of translate file entries.  (int)                         */
  121.     /*  The number of translate file entries of:                         */
  122.     /*    Length of Null Terminated Key String (including NULL) (int)    */
  123.     /*    Null Terminated Key String                                     */
  124.     /*    Length of Null Terminated Result String (including NULL) (int) */
  125.     /*    Null Terminated Result String                                  */
  126.     /*  <eof>                                                            */
  127.  
  128.     node = XlateBase;  /* Set node to XlateBase */
  129.  
  130.     /* Write the data to the Binary Output file */
  131.     /* Signature */
  132.     if(fwrite(".TRB", 4, 1, bf) != 1)
  133.       printf("Speed_up: Error Writing Signature!\n");
  134.     if(fwrite(&XlateLines, sizeof(XlateLines), 1, bf) != 1)
  135.       printf("Speed_up: Error Writing Number of Key/Result Pairs!\n");
  136.  
  137.     for(i = 0; i < XlateLines ; i++)
  138.     {
  139.       length = strlen(node->translate_key) + 1;
  140.       if(fwrite(&length, sizeof(length), 1, bf) != 1)
  141.         printf("Speed_up: Error Writing Length of Key Value.\n");
  142.  
  143.       if(fwrite(node->translate_key,
  144.                 strlen(node->translate_key) + 1, 1, bf) != 1)
  145.         printf("Speed_up: Error Writing Key Value number %d.\n", i);
  146.  
  147.       length = strlen(node->translated_value) + 1;
  148.       if(fwrite(&length, sizeof(length), 1, bf) != 1)
  149.         printf("Speed_up: Error Writing Length of Result Value.\n");
  150.       if(fwrite(node->translated_value,
  151.                 strlen(node->translated_value) + 1, 1, bf) != 1)
  152.         printf("Speed_up: Error Writing Result Value number %d.\n", i);
  153.  
  154.       node++;                   /* advance to next entry. */
  155.     }
  156.     /* Done, close destination file */
  157.     fclose(bf);
  158.   }
  159.   printf("Speed_up: Finished.\n");
  160. }
  161. /* end 'main()' */
  162.